﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace ZHDJ.Data.Models.SMS
{
    public class PartySmsSendRecord : EntityBase
    {
        /// <summary>
        /// 短信来源
        /// </summary>
        public string Source { get; set; }

        /// <summary>
        /// 手机号码
        /// </summary>
        public string MobileNo { get; set; }

        /// <summary>
        /// 短信内容
        /// </summary>
        public string SMSContent { get; set; }

        /// <summary>
        /// 短信优先级
        /// </summary>
        public int SMSPriority { get; set; }

        /// <summary>
        /// 消息批次号
        /// </summary>
        public string MSGGroup { get; set; }

        /// <summary>
        /// 是否需要上行
        /// </summary>
        public bool IsMo { get; set; }

        /// <summary>
        /// 短信批次号
        /// </summary>
        public string BatchNumber { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime? CreateTime { get; set; }

        /// <summary>
        /// 发送时间
        /// </summary>
        public DateTime? SendTime { get; set; }

        /// <summary>
        /// 发送状态
        /// </summary>
        public string SendState { get; set; }

        /// <summary>
        /// 发送次数
        /// </summary>
        public int SendTimes { get; set; }

        /// <summary>
        /// 短信提交结果
        /// </summary>
        public string SMSSubmitResult { get; set; }

        /// <summary>
        /// 提交报告结果
        /// </summary>
        public string SubmitReportResult { get; set; }

        /// <summary>
        /// 状态报告结果
        /// </summary>
        public string StateReportResult { get; set; }

        public static DataSet GetGridData(int pageIndex, int pageSize, string mobileNo, string state)
        {
            List<MySqlParameter> parameters = new List<MySqlParameter>()
            {
                new MySqlParameter(){DbType = DbType.Int32,ParameterName = "Begin",Value = (pageIndex)*pageSize},
                new MySqlParameter(){DbType = DbType.Int32,ParameterName = "PageSize",Value = pageSize},
                new MySqlParameter(){DbType = DbType.String,ParameterName = "MobileNo",Value = string.Format("%{0}%",mobileNo)},
                new MySqlParameter(){DbType = DbType.String,ParameterName = "SendState",Value = state}
            };

            StringBuilder sb = new StringBuilder();
            sb.Append(@"select SQL_CALC_FOUND_ROWS  r.ID,Source,MobileNo,SMSContent,SMSPriority,MSGGroup,IsMo,BatchNumber,CreateTime,SendTime,SendState,
                        case SendState when '0' then '未发送' when '1' then '发送中' when '2' then '发送成功' when '-1' then '发送失败' end as SendStateName,
                        SendTimes,SMSSubmitResult,SubmitReportResult,StateReportResult
                        from tblpartysmssendrecord r
                        where 1=1");

            if (!string.IsNullOrEmpty(mobileNo))
            {
                sb.Append(" and MobileNo like ?MobileNo");
            }
            if (!string.IsNullOrEmpty(state))
            {
                sb.Append(" and SendState=?SendState");
            }

            sb.Append(" order by CreateTime desc LIMIT ?Begin ,?PageSize;SELECT FOUND_ROWS();");
            return MySqlHelper.ExecuteDataSet(sb.ToString(), parameters.ToArray());
        }
    }
}
